# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py
# RUN: llc -mtriple=aarch64 -global-isel -run-pass=instruction-select -verify-machineinstrs %s -o - | FileCheck %s

--- |
  define void @lr_other_block() { ret void }
  define void @already_live_in() { ret void }
  define void @multi_use() { ret void }
  declare i8* @llvm.returnaddress(i32 immarg)
...
---
name:            lr_other_block
alignment:       4
legalized:       true
regBankSelected: true
tracksRegLiveness: true
body:             |
  ; CHECK-LABEL: name: lr_other_block
  ; CHECK: bb.0:
  ; CHECK:   successors: %bb.1(0x80000000)
  ; CHECK:   liveins: $w0, $x0, $lr
  ; CHECK:   [[COPY:%[0-9]+]]:gpr64 = COPY $lr
  ; CHECK: bb.1:
  ; CHECK:   $lr = COPY [[COPY]]
  ; CHECK:   XPACLRI implicit-def $lr, implicit $lr
  ; CHECK:   [[COPY1:%[0-9]+]]:gpr64 = COPY $lr
  ; CHECK:   $x0 = COPY [[COPY1]]
  ; CHECK:   RET_ReallyLR implicit $x0
  ; LR should be added as a livein to the entry block.

  bb.0:
    ; We should have lr as a livein to bb.0, and a copy from LR.
    liveins: $w0, $x0
    G_BR %bb.1
  bb.1:
    %4:gpr(p0) = G_INTRINSIC intrinsic(@llvm.returnaddress), 0
    $x0 = COPY %4
    RET_ReallyLR implicit $x0
...
---
name:            already_live_in
alignment:       4
legalized:       true
regBankSelected: true
tracksRegLiveness: true
body:             |
  ; CHECK-LABEL: name: already_live_in
  ; CHECK: bb.0:
  ; CHECK:   successors: %bb.1(0x80000000)
  ; CHECK:   liveins: $w0, $x0, $lr
  ; CHECK:   [[COPY:%[0-9]+]]:gpr64 = COPY $lr
  ; CHECK: bb.1:
  ; CHECK:   $lr = COPY [[COPY]]
  ; CHECK:   XPACLRI implicit-def $lr, implicit $lr
  ; CHECK:   [[COPY1:%[0-9]+]]:gpr64 = COPY $lr
  ; CHECK:   $x0 = COPY [[COPY1]]
  ; CHECK:   RET_ReallyLR implicit $x0
  ; We should not have LR listed as a livein twice.

  bb.0:
    liveins: $w0, $x0, $lr
    G_BR %bb.1
  bb.1:
    %4:gpr(p0) = G_INTRINSIC intrinsic(@llvm.returnaddress), 0
    $x0 = COPY %4
    RET_ReallyLR implicit $x0
...

# Check copies are inserted in the right places when selecting
# multiple uses in different blocks
---
name:            multi_use
alignment:       4
legalized:       true
regBankSelected: true
tracksRegLiveness: true
body:             |
  ; CHECK-LABEL: name: multi_use
  ; CHECK: bb.0:
  ; CHECK:   successors: %bb.1(0x80000000)
  ; CHECK:   liveins: $w0, $x0, $lr
  ; CHECK:   [[COPY:%[0-9]+]]:gpr64 = COPY $lr
  ; CHECK:   $lr = COPY [[COPY]]
  ; CHECK:   XPACLRI implicit-def $lr, implicit $lr
  ; CHECK:   [[COPY1:%[0-9]+]]:gpr64 = COPY $lr
  ; CHECK: bb.1:
  ; CHECK:   $x0 = COPY [[COPY1]]
  ; CHECK:   $lr = COPY [[COPY]]
  ; CHECK:   XPACLRI implicit-def $lr, implicit $lr
  ; CHECK:   [[COPY2:%[0-9]+]]:gpr64 = COPY $lr
  ; CHECK:   RET_ReallyLR implicit [[COPY2]]
  bb.0:
    liveins: $w0, $x0, $lr
    %0:gpr(p0) = G_INTRINSIC intrinsic(@llvm.returnaddress), 0
    G_BR %bb.1
  bb.1:
    $x0 = COPY %0
    %1:gpr(p0) = G_INTRINSIC intrinsic(@llvm.returnaddress), 0
    RET_ReallyLR implicit %1
...
